package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;


    /**
     * 根据分类查询查询规格参数组(规格参数组和分类是关联的)
     * 一个分类对应一个规格参数模板,一个规格参数模板下面有多个规格参数组(也就是说一个分类有多个规格参数组)
     *
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid) {
        List<SpecGroup> specGroups = this.specificationService.querySpecGroups(cid);
        if (specGroups != null && specGroups.size() != 0) {
            return ResponseEntity.status(HttpStatus.OK).body(specGroups);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 规格组的修改
     */

    @PutMapping("group")
    public ResponseEntity<Void> saveSpecGroups(@RequestBody SpecGroup specGroup) {
        this.specificationService.saveSpecGroups(specGroup);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 规格组的删除
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteSpecGroups(@PathVariable("id") Long id) {
        this.specificationService.deleteSpecGroups(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 规格组的新增
     */
    @PostMapping("group")
    public ResponseEntity<Void> addSpecGroups(@RequestBody SpecGroup specGroup) {
        this.specificationService.addSpecGroups(specGroup);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 从规格组切换到规格参数
     */

    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false)Long cid,
            @RequestParam(value = "searching",required = false)Boolean searching,
            @RequestParam(value = "generic",required = false)Boolean generic) {
        List<SpecParam> specParams = this.specificationService.querySpecParams(gid,cid,searching,generic);
        if (specParams != null && specParams.size() != 0) {
            return ResponseEntity.status(HttpStatus.OK).body(specParams);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
